package cn.xz.zysjz.activity;

import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.chad.library.adapter.base.listener.OnItemLongClickListener;

import java.util.ArrayList;
import java.util.Objects;

import cn.xz.zysjz.R;
import cn.xz.zysjz.adapter.SJJListAdapter;
import cn.xz.zysjz.adapter.UserListAdapter;
import cn.xz.zysjz.bean.CommonLib;
import cn.xz.zysjz.bean.HomeListBean;
import cn.xz.zysjz.bean.SJBean;
import cn.xz.zysjz.bean.UserBean;
import cn.xz.zysjz.databinding.ActivityGlSjBinding;
import cn.xz.zysjz.databinding.ActivityGlYonghuBinding;
import cn.xz.zysjz.dialog.ShowDialog;
import cn.xz.zysjz.utils.SessionUtils;

public class GLSJActivity extends AppCompatActivity {

    ActivityGlSjBinding mBinding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mBinding = DataBindingUtil.setContentView(this, R.layout.activity_gl_sj);
        if (Build.VERSION.SDK_INT >= 21) {
            this.getWindow()
                    .setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
        }
        //列表设置
        initRV();
        mBinding.viewBack.setOnClickListener(v->{
            finish();
        });
    }

    private void initRV(){
        //列表相关数据
        LinearLayoutManager ms = new LinearLayoutManager(this);
        SJJListAdapter mAdapter = new SJJListAdapter(R.layout.item_sjj_list);
        mBinding.rvList.setLayoutManager(ms);
        mBinding.rvList.setAdapter(mAdapter);
        //获取商家列表
        ArrayList<SJBean> userList = SessionUtils.getList(CommonLib.SJJ_L, SJBean.class);
        mAdapter.setList(userList);
        //通过
        mAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                SJBean data = (SJBean) adapter.getData().get(position);
                ShowDialog.show1("确定审核通过吗?", GLSJActivity.this, new ShowDialog.OnShowClick() {
                    @Override
                    public void click() {
                        ArrayList<UserBean> userList = SessionUtils.getList(CommonLib.USER_DATA, UserBean.class);
                        for (UserBean userBean: userList){
                            if (Objects.equals(userBean.getName(), data.getName())){
                                userBean.setSj(true);
                                data.setGuo(true);
                            }
                        }
                        SessionUtils.commitList(CommonLib.USER_DATA,userList);
                        adapter.notifyItemChanged(position);
                    }
                });

            }
        });

        //删除
        mAdapter.setOnItemLongClickListener(new OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(@NonNull BaseQuickAdapter adapter, @NonNull View view, int position) {
                SJBean data = (SJBean) adapter.getData().get(position);
                ShowDialog.show1("确定删除该商家吗?", GLSJActivity.this, new ShowDialog.OnShowClick() {
                    @Override
                    public void click() {
                        ArrayList<UserBean> userList = SessionUtils.getList(CommonLib.USER_DATA, UserBean.class);
                        int ttt = -1;
                        for (int i = 0; i < userList.size(); i ++){
                            if (Objects.equals(userList.get(i).getName(), data.getName())){
                                ttt = i;
                            }
                        }
                        if (ttt != -1){

                            ArrayList<SJBean> userList1 = SessionUtils.getList(CommonLib.SJJ_L, SJBean.class);
                            int m = -1;
                            for (int o = 0; o < userList1.size(); o++){
                                if (Objects.equals(userList1.get(o).getName(), data.getName())){
                                    m = o;
                                }
                            }
                            if (m != -1){
                                userList1.remove(m);
                                SessionUtils.commitList(CommonLib.SJJ_L, userList1);
                            }


                            userList.get(ttt).setSj(false);
                            adapter.removeAt(position);
                        }
                    }
                });
                return false;
            }
        });

    }
}